package com.demo.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @author 李天航
 * 全局切片
 */
@Aspect
@Service
@Order(2)
public class GlobalAspect {

    @Pointcut("execution(private * com.demo.controller.*.*(..))")
    public void handleException(){}

    @Before("handleException()")
    public void beforeCut()
    {
        System.out.println("before");
    }
    @After("handleException()")
    public void afterCut()
    {
        System.out.println("after");
    }
    @AfterReturning(pointcut = "handleException()",returning = "obj")
    public void afterReturning(JoinPoint joinPoint,Object obj)
    {
        System.out.println("afterReturn");
        System.out.println(obj);
    }
    @AfterThrowing(pointcut = "handleException()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Exception e)
    {
        System.out.println("afterThrowing");
        e.printStackTrace();
    }

    @Around("handleException()")
    public Object aroundCut(ProceedingJoinPoint proceedingJoinPoint ) {
        System.out.println("aroundBegin");
        System.out.println(Arrays.toString(proceedingJoinPoint.getArgs()));
        System.out.println(proceedingJoinPoint.getKind());
        System.out.println(proceedingJoinPoint.getSignature());
        proceedingJoinPoint.getTarget();
        try {
            proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("aroundEnd");
        return "ok";
    }
}
